from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

import urllib.request
import os ,stat
import pyautogui

from time import sleep
import re
import codecs
import sys
import os
import platform


from disease_model import *
from test_model import *
from mysql import *

class Disease_img:
    # 当前id
    __current_id = 8424

    __max_id = 8426

    # 当前条数，用来处理点击到第几个了
    __current_index = 0

    model = None

    # 记录采集位置，格式：采集当前页数的第几条
    log_file = sys.path[0] + '/log_diseaseimg.data'

    def init_chrome(self):
        '''
        初始化浏览器
        '''

        # options
        chrome_options = webdriver.ChromeOptions()


        #chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--no-sandbox')

        proxy = None

        if platform.system() == 'Windows':
            chrome_options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
        else:
            chrome_options.add_argument('--headless')
            chrome_options.add_argument('--disable-gpu')
            chrome_options.add_argument('--no-sandbox')
            # proxy = self.get_proxy_ip()

        if proxy:
            print('获取代理IP：', proxy)
            # 设置代理
            chrome_options.add_argument('--proxy-server=%s' % proxy)

        self.__browser = webdriver.Chrome(options=chrome_options)
        self.__browser.set_window_size(1100, 1050)

    def start(self):

        #
        #
        # sys.exit()
        # 读取日志数据
        self.get_collect_log()

        self.collect_list()

    def collect_list(self):
        self.disease = Disease_Model()
        self.diseaseimg = Diseaseimg_Model()
        # 查询当前疾病下状态为0的数据，进行循环处理
        diseaseimgs = self.diseaseimg.fetch('status=0 and `did`='+str(self.__current_id))
        if(len(diseaseimgs) > 0):
            i = 0
            for img in diseaseimgs:
                if(i < self.__current_index):
                    i = i + 1
                    continue
                url = urllib.parse.unquote(img['url'])
                self.__start_page = re.findall("imgurl=(.+?)&imgrefurl=", url)[0]
                try:
                    self.init_chrome()
                    self.__browser.get(self.__start_page)
                except WebDriverException as err:
                    print('初始化出错')
                    os._exit(0)

                if self.is_element_present(By.TAG_NAME, 'img'):
                    # 右键另存
                    pic = self.__browser.find_element(By.TAG_NAME, 'img')
                    action = ActionChains(self.__browser).move_to_element(pic)  # 移动到该元素
                    action.context_click(pic)  # 右键点击该元素
                    action.perform()  # 执行
                    pyautogui.typewrite(['v'])  # 敲击V进行保存
                    # 单击图片另存之后等1s敲回车
                    sleep(1)
                    pyautogui.typewrite(['enter'])
                    self.__current_index = int(self.__current_index) + 1
                    self.save_collect_log()


        if int(self.__current_id) > int(self.__max_id):
            self.__current_id = self.__current_id - 1
            self.__current_index = 0
            # 保存日志
            self.save_collect_log()
            # 继续采集下一页
            self.collect_list()
        else:
            print('没有下一页，退出')

            self.__current_id = 0
            self.__current_index = 0
            # 保存日志
            self.save_collect_log()
            # 退出
            self.quit_process()

    # 判断元素是否存在
    def is_element_present(self, how, what):
        try:
            self.__browser.find_element(by=how, value=what)
        except NoSuchElementException:
            return False
        return True

    def save_collect_log(self):
        '''
        保存抓取位置
        '''
        with codecs.open(self.log_file, 'w', encoding='utf-8') as file:
            file_content = str(self.__current_id) + '-' + str(self.__current_index)
            file.write(file_content)
            file.close()

    def get_collect_log(self):
        '''
        获取抓取位置
        '''
        self.__current_id = 1

        if os.path.exists(self.log_file):
            with codecs.open(self.log_file, 'r', encoding='utf-8') as file:
                file_content = file.read(100)
                file.close()
                if len(file_content) > 0:
                    contents = file_content.split("-")
                    self.__current_id = int(contents[0])
                    self.__current_index = int(contents[1])
                    print('读取文件日志完成: 当前页：%s，当前条数：%s' % (self.__current_id, self.__current_index))
                else:
                    raise '日志格式不正确'
        else:
            print('日志文件不存在...')

    def quit_process(self):
        sys.exit()





starter = Disease_img()
starter.start()